﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyFeer.Widget.Grid.Cell;
using MyFeer.Lib.common;

namespace MyFeer.Widget.Grid
{
    public class GridModel
    {
        private List<IntegerRect> unites=new List<IntegerRect>();
        private CellModel[,] cells; 
        private List<int> rows;
        private List<int> columns;
        public GridModel(int rowSize, int colSize)
        {
            this.rows = new List<int>();
            this.columns = new List<int>();
            cells = new CellModel[rowSize, rowSize];
            int rowHeight = GridConst.DefaultRowHeight;
            for (int i = 0; i < rowSize; i++)
            {
                this.rows.Add(rowHeight);
            }
            int colWidth = GridConst.DefaultColumnWidth;
            for (int i = 0; i < colSize; i++)
            {
                this.columns.Add(colWidth);
            }
        }

        internal List<int> getRowPs()
        {
            List<int> rowPos = new List<int>();
            int posY = 0;
            for (int i = 0; i < this.rows.Count; i++)
            {
                posY+= this.rows[i];
                rowPos.Add(posY);
                
            }
            return rowPos;
        }
        internal List<int> getColumnPos()
        {
            List<int> colPos = new List<int>();
            int posX = 0;
            for (int i = 0; i < this.columns.Count; i++)
            {
                posX += this.columns[i];
                colPos.Add(posX);
            }
            return colPos;
        }

        public List<IntegerRect> Unites
        {
            get { return this.unites; }
        }

        public void setRowHeight(int rowIndex, int height)
        {
            this.rows[rowIndex] = height;
        }

        public void setColumnWidth(int colIndex, int width)
        {
            this.columns[colIndex] = width;
            
        }
        public CellModel getCellModel(int rowIndex, int colIndex)
        {
            CellModel cellModel=this.cells[rowIndex, colIndex];
            if (cellModel == null)
            {
                cellModel = new CellModel();
                cellModel.Left = colIndex;
                cellModel.Top = rowIndex;
                return cellModel;
            }
            else
            {
                return cellModel;
            }
        }
        public void setCellModel(int rowIndex, int colIndex, CellModel cellModel)
        {
            this.cells[rowIndex, colIndex] = cellModel;
        }

        public int getColumnWidth(int colIndex)
        {
            return this.columns[colIndex];
        }

        public int getRowHeight(int rowIndex)
        {
            return this.rows[rowIndex];
        }

        /// <summary>
        /// 插入行
        /// </summary>
        /// <param name="rowIndex"></param>
        public void insertRow(int rowIndex,int rowSize)
        {

            this.changeRect(rowIndex, rowSize, 0, 0);
        }
        /// <summary>
        /// 删除行
        /// </summary>
        /// <param name="rowIndex"></param>
        /// <param name="rowSize"></param>
        public void removeRow(int rowIndex, int rowSize)
        {
            this.changeRect(rowIndex, -1 * rowSize, 0, 0);
        }
        /// <summary>
        /// 删除列
        /// </summary>
        /// <param name="colIndex"></param>
        /// <param name="colSize"></param>
        public void removeColumn(int colIndex, int colSize)
        {
            this.changeRect(0, 0, colIndex, -1 * colSize);
        }

        /// <summary>
        /// 插入列
        /// </summary>
        /// <param name="colIndex"></param>
        public void insertColumn(int colIndex,int colSize)
        {
            this.changeRect(0, 0, colIndex, colSize);
        }
        /// <summary>
        ///修改表格区域大小
        /// </summary>
        /// <param name="rowIndex">行位置</param>
        /// <param name="rowSize">在行位置插入或删除N行</param>
        /// <param name="colIndex"></param>
        /// <param name="colSize"></param>
        private void changeRect(int rowIndex, int rowSize, int colIndex, int colSize)
        {
            CellModel[,] oldCells = this.cells;
            CellModel[,] cells = new CellModel[this.cells.GetLength(0) + rowSize, this.cells.GetLength(1)+colSize];
            int curRowIndex = 0;
            for (int i = 0; i < this.cells.GetLength(0); i++)
            {
                curRowIndex=i;
                if (i >= rowIndex && rowSize!=0)
                {
                    if (rowSize < 0 && i <= rowIndex - rowSize)
                    {
                        continue;
                    }
                    else
                    {
                        curRowIndex = i + rowSize;
                    }
                }
                for (int j = 0; j < this.cells.GetLength(1); j++)
                {
                    int curColIndex = j;
                    if (j >= colIndex && colSize!=0)
                    {
                        if (colSize < 0 && j <= colIndex - colSize)
                        {
                            continue;
                        }
                        else
                        {
                            curColIndex = j + colSize;
                        }
                    }
                    CellModel cellModel=this.cells[i, j];
                    cellModel.Left = curColIndex;
                    cellModel.Top = curRowIndex;
                    cells[curRowIndex, curColIndex] = this.cells[i, j];
                }
                
            }
            if (rowSize>0)
            {
                List<int> newRows=new List<int>();
                for (int i = 0; i < rowSize;i++ )
                {
                    newRows.Add(GridConst.DefaultRowHeight);
                }
                this.rows.InsertRange(rowIndex, newRows);
            }
            else if (rowSize < 0)
            {
                this.rows.RemoveRange(rowIndex, Math.Abs(rowSize));
            }
            if (colSize > 0)
            {
                List<int> newCols = new List<int>();
                for (int i = 0; i < colSize; i++)
                {
                    newCols.Add(GridConst.DefaultColumnWidth);
                }
                this.columns.InsertRange(colIndex, newCols);
            }
        }
    }
}
